package cn.gd.controller.systemAdministration.roleManagement;

import cn.gd.pojo.Roles;
import cn.gd.service.systemAdministration.Role.RoleService;
import cn.gd.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询角色
     * @param pageNum 起始数
     * @param pageSize 末数
     * @return
     */
    @GetMapping("/queryAllRole/{pageNum}/{pageSize}")
    public Result<PageInfo<Roles>> queryAllRole(
            @PathVariable int pageNum,
            @PathVariable int pageSize){
        return Result.ok(roleService.queryAllRoles(pageNum,pageSize));
    }

    /**
     * 根据角色ID修改角色信息并将其他的排序加一
     * @param role
     * @return
     */
    @PutMapping("/updateRoleByRoleId")
    public Result<Boolean> updateRoleByRoleId(@RequestBody Roles role){
        return Result.ok(roleService.updateRole(role));
    }

    /**
     * 判断该角色有没有用户使用，有则不能删除并返回提示信息，没有则根据ID删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/deleteRoleByRoleId")
    public Result<Boolean> deleteRoleByRoleId(Integer roleId){
        Boolean b = roleService.deleteRoleByRoleId(roleId);
        Result<Boolean> result;
        if(!b){
             result = Result.error();
             result.setMsg("有用户使用无法删除！！");
        }else{
            result = Result.ok(b);
        }
        System.out.println(result.getMsg());
        return result;
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public Result<Boolean> addRole(@RequestBody Roles role){
        return Result.ok(roleService.addRole(role));
    }
}
